import os
import shutil

import facade
from xjlibrary.mdatetime.mtime import getTodayDate
from xjlibrary.our_file_dir import BaseDir

curPath = BaseDir.get_file_dir_absolute(__file__)
configfile = BaseDir.get_new_path(curPath, "db.ini")

dealSqlFieldsFunc = lambda text: "'" + text.replace("'", "''") + "'" if text is not None else 'null'


class ExportNewData(object):
    def __init__(self, export_date):
        self.logger = facade.get_streamlogger()
        self.mysqlutils = facade.MysqlUtiles(configfile,
                                             "db",
                                             logger=self.logger)
        temp_db3 = os.path.join(curPath, "db3", "template_DB3.db3")
        new_db3_out = os.path.join(curPath, "newDB3")
        self.new_db3_path = self.copyTempDB3(temp_db3, new_db3_out)
        self.db3_zt = facade.Sqlite3Utiles(logger=self.logger).Sqlite3DBConnectFromFilePath(
            self.new_db3_path, encoding="utf-8")
        self.export_date = export_date

    def copyTempDB3(self, temp_path, copy_out):
        """
        拷贝创建新的db3文件
        :param temp_path: 模板db3文件路径
        :param copy_out: 新的db3输出路径
        :return: 新db3路径
        """
        today_str = str(getTodayDate())
        if not os.path.isfile(temp_path) or not os.path.isdir(copy_out):
            raise FileNotFoundError
        new_db3_name = 'zt_tandfbook_' + today_str + '.db3'
        new_db3_path = os.path.join(copy_out, new_db3_name)
        shutil.copyfile(temp_path, new_db3_path)
        return new_db3_path

    def getNewDataFromDB(self, batch):
        """
        从总数据中提取更新字段
        :param conn: 数据库连接对象
        :param batch: 批次时间戳
        :return: 符合要求的结果生成器
        """
        Sql = "SELECT * FROM allfields WHERE batch >= %s" % batch
        result = self.mysqlutils.SelectFromDB(Sql)
        result_gen = (item[:-2] for item in result)
        return result_gen

    def dealItemTuple(self, item_tuple):
        item_list = [dealSqlFieldsFunc(field) for field in item_tuple]
        return tuple(item_list)

    def start(self):
        new_items_gen = self.getNewDataFromDB(self.export_date)
        Sql_temp = "INSERT OR IGNORE INTO modify_title_info_zt VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
        lists = []
        for new_item in new_items_gen:
            lists.append(new_item)
            if len(lists) > 10000:
                self.db3_zt.ExeSqlliteMany(Sql_temp, lists)
                lists.clear()
        if len(lists) > 0:
            self.db3_zt.ExeSqlliteMany(Sql_temp, lists)
            lists.clear()


if __name__ == "__main__":
    data = ExportNewData(export_date="2019101200")
    data.start()
